﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using TRPOP.CRM.Component.Model;
using TRPOP.Common;

namespace TRPOP.CRM.Component.DALSQLServer
{
    public class FollowUpCategoryDAL
    {

        private static TRPOP.ComSqlDAL.ICommonDAL CommonDAL = new TRPOP.ComSqlDAL.CommonDAL();
        #region 私有的方法
        ///	<summary>
        ///	从Table取数据到实体
        ///	</summary>
        ///	<param name="oDataRow">数据行</param>
        ///	<returns>返回实体</returns>
        private static FollowUpCategoryInfo ParseRowTomodel(DataRow oDataRow)
        {
            FollowUpCategoryInfo oInfo = new FollowUpCategoryInfo();
            oInfo.nvcFollowUpCategoryName = oDataRow["nvcFollowUpCategoryName"] is DBNull ? "" : (string)oDataRow["nvcFollowUpCategoryName"];
            oInfo.nvcDiscription = oDataRow["nvcDiscription"] is DBNull ? "" : (string)oDataRow["nvcDiscription"];
            oInfo.bIsDelete = oDataRow["bIsDelete"] is DBNull ? false : (bool)oDataRow["bIsDelete"];
            oInfo.iSortOrder = oDataRow["iSortOrder"] is DBNull ? 0 : (int)oDataRow["iSortOrder"];
            oInfo.iFollowUpCategoryID = oDataRow["iFollowUpCategoryID"] is DBNull ? 0 : (int)oDataRow["iFollowUpCategoryID"];
            return oInfo;
        }


        ///	<summary>
        ///	通用的添加存储过程的变量
        ///	</summary>
        ///	<param name="osdDataBase">SqlDatabase</param>
        ///	<param name="odbCommand">DbCommand</param>
        ///	<param name="oInfo">实体</param>
        private static FollowUpCategoryInfo ParseModeltoParam(SqlDatabase odbDataBase, DbCommand odbCommand, FollowUpCategoryInfo oInfo)
        {
            odbDataBase.AddInParameter(odbCommand, "@nvcFollowUpCategoryName", System.Data.DbType.String, oInfo.nvcFollowUpCategoryName);
            odbDataBase.AddInParameter(odbCommand, "@nvcDiscription", System.Data.DbType.String, oInfo.nvcDiscription);
            odbDataBase.AddInParameter(odbCommand, "@bIsDelete", System.Data.DbType.Boolean, oInfo.bIsDelete);
            odbDataBase.AddInParameter(odbCommand, "@iSortOrder", System.Data.DbType.Int32, oInfo.iSortOrder);
            odbDataBase.AddParameter(odbCommand, "@iFollowUpCategoryID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, oInfo.iFollowUpCategoryID);
            return oInfo;
        }


        #endregion

        ///	<summary>
        ///	 更新或添加
        ///	</summary>
        ///	<param name="oInfo"></param>
        ///	<returns>bool</returns>
        public bool Save(FollowUpCategoryInfo oInfo)
        {
            bool flag = false;
            try
            {
                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetStoredProcCommand("FollowUpCategory_Save");
                ParseModeltoParam(odbDataBase, odbCommand, oInfo);
                odbDataBase.ExecuteNonQuery(odbCommand);
                oInfo.iFollowUpCategoryID = (int)odbDataBase.GetParameterValue(odbCommand, "@iFollowUpCategoryID");
                flag = true;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return flag;
        }

        ///	<summary>
        ///	根据Primary Key获取信息实体
        ///	</summary>
        ///	<param name="intiFollowUpCategoryID"></param>
        ///	<returns>FollowUpCategoryInfo</returns>
        public FollowUpCategoryInfo GetInfoById(int intiFollowUpCategoryID)
        {
            FollowUpCategoryInfo oInfo = null;
            string strSqlString = "SELECT * FROM FollowUpCategory WHERE  iFollowUpCategoryID=" + intiFollowUpCategoryID + "    ";
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSqlString, SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                if (oDataTable.Rows.Count == 1)
                {
                    oInfo = ParseRowTomodel(oDataTable.Rows[0]);
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }

        ///	<summary>
        ///	 根据Primary Key删除信息实体
        ///	</summary>
        ///	<param name="intiFollowUpCategoryID"></param>
        ///	<returns>bool</returns>
        public bool DeleteInfoById(int intiFollowUpCategoryID)
        {
            string strSqlString = "DELETE FROM FollowUpCategory WHERE   iFollowUpCategoryID=" + intiFollowUpCategoryID + "    ";
            try
            {
                return CommonDAL.ExeSQLQuery(strSqlString, SystemInfoHelper.getSystemInfo().DataBaseConnection) == EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
        }

        ///	<summary>
        ///	 获取信息列表（无分页）
        ///	</summary>
        ///	<param name="intTopn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="strCondition">自定义条件，要以And开头</param>
        ///	<param name="strOrderby">排序，不需要Order by </param>
        ///	<returns>List<FollowUpCategoryInfo></returns>
        public List<FollowUpCategoryInfo> GetListByCondition(int intTopn, string strCondition, string strOrderby)
        {
            List<FollowUpCategoryInfo> oList = new List<FollowUpCategoryInfo>();
            string strSql = CommonDAL.strbuildSqlForList(intTopn, strCondition, strOrderby, "FollowUpCategory");
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSql, SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }

        ///	<summary>
        ///	获取分页信息
        ///	</summary>
        /// <param name="intPageNumber">当前页页码</param>
        /// <param name="intPageSize">分页大小</param>
        /// <param name="strCondition">查询条件 and ....</param>
        /// <param name="strOrderby">排序的字段名 id desc</param>
        /// <param name="intRecordCount">总记录数</param>
        ///	<returns>List<FollowUpCategoryInfo></returns>
        public List<FollowUpCategoryInfo> GetListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            List<FollowUpCategoryInfo> oList = new List<FollowUpCategoryInfo>();
            try
            {
                DataSet oDataSet = CommonDAL.GetListByPager("FollowUpCategory", "*", intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount, SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }


    }
}
